import React from 'react';

class Img extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      src: props.src
    }
  }
  componentWillReceiveProps(nextprops) {
    this.setState({src: nextprops.src});
  }

  // 图片加载失败，将默认值赋值给src
  handleImageErrored() {
    this.setState({
      src: ''
    });
  }

  render() {
    const {src} = this.state
    return (
      src && <img style={this.props.style}
           src={this.state.src}
           onError={this.handleImageErrored.bind(this)}
      />
    );
  }
}
export default Img;